package com.tony.demo.ui.performance.xnyh.draw;

import android.content.res.Resources;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.puhui.lib.utils.DMLog;
import com.tony.demo.R;
import com.tony.demo.ui.performance.xnyh.draw.bean.Chat;
import com.tony.demo.ui.performance.xnyh.draw.bean.Droid;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Date;


/**
 * Fragment that encapsulates creating {@link Chat} objects and displaying them using a
 * {@link ListView} layout.
 * Creates seed data consisting of a handful of chats authored by a few authors.
 */
public class ChatsFragment extends Fragment {
    protected static int MILLISECONDS_PER_SECOND = 1000;
    protected static int SECONDS_PER_MINUTE = 60;
    private boolean isOptimize = false;

    public ChatsFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        // Create a list of chats and populate it with hardcoded data.
        ArrayList<Chat> chats = new ArrayList<>();
        populateChats(chats);

        // Create the adapter that provides values to the UI Widgets.
        ChatAdapter adapter = new ChatAdapter(getActivity(), chats, isOptimize);
        DMLog.e(this.getClass().getCanonicalName(), "isOptimize = " + isOptimize);

        View rootView = inflater.inflate(R.layout.fragment_chats, container, false);

        // Find the ListView that holds all the chat messages and attach it to the adapter,
        ListView listView = (ListView) rootView.findViewById(R.id.listview_chats);
        listView.setAdapter(adapter);

        return rootView;
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void isOptimize(String isOptimize) {
        this.isOptimize = isOptimize.equals("true");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private Date getTimeInPast(int minutesAgo) {
        return new Date(new Date().getTime() -
                (minutesAgo * SECONDS_PER_MINUTE * MILLISECONDS_PER_SECOND));
    }

    // Creates hardcoded chat objects.
    private void populateChats(ArrayList<Chat> chats) {
        Resources res = getResources();
        Droid alex = new Droid("alex", res.getColor(R.color.alex_color));
        Droid joanna = new Droid("joanna", res.getColor(R.color.joanna_color), R.mipmap.joanna);
        Droid shailen = new Droid("shailen", res.getColor(R.color.shailen_color),
                R.mipmap.shailen);

        chats.add(new Chat(alex, "Lorem ipsum dolor sit amet, orci nullam cra",
                getTimeInPast(15)));

        chats.add(new Chat(joanna, "Omnis aptent magnis suspendisse ipsum, semper egestas " +
                "magna auctor maecenas",
                getTimeInPast(11)));

        chats.add(new Chat(shailen, "eu nibh, rhoncus wisi posuere lacus, ad erat egestas " +
                "quam, magna ante ultricies sem",
                getTimeInPast(9)));

        chats.add(new Chat(alex, "rhoncus wisi posuere lacus, ad erat egestas quam, magna " +
                "ante ultricies sem lacus",
                getTimeInPast(8)));

        chats.add(new Chat(shailen, "Enim justo nisl sit proin, quis vestibulum vivamus " +
                "suscipit penatibus et id, tempus mauris a lacus blandit, aenean praesent " +
                "arcu scelerisque sociosqu. Nonummy at ut ullamcorper nulla, ligula id, " +
                "nullam donec nisl ante turpis duis mauris, dolor imperdiet a inceptos aliquam",
                getTimeInPast(8)));

        chats.add(new Chat(joanna, "Omnis aptent magnis.",
                getTimeInPast(7)));

        chats.add(new Chat(alex, "Metus tincidunt sit in urna.",
                getTimeInPast(6)));

        chats.add(new Chat(shailen, "Non blandit nulla dapibus, vitae quisque sed cras mi " +
                "leo condimentum sociosqu quis sed pharetra",
                getTimeInPast(4)));

        chats.add(new Chat(joanna, "Enim justo nisl sit proin, quis vestibulum vivamus " +
                "suscipit penatibus et id, tempus mauris a lacus blandit, aenean praesent " +
                "arcu scelerisque sociosqu. Nonummy at ut ullamcorper nulla, ligula id, " +
                "nullam donec nisl ante turpis duis mauris, dolor imperdiet a inceptos.",
                getTimeInPast(3)));
    }
}